package com.hedl.practice.diyJava.multithreading.Interrupt_thread.practice.example1;

import com.hedl.practice.diyJava.multithreading.Interrupt_thread.practice.example1.entity.MyThread;

import java.util.concurrent.TimeUnit;

/**
 * @author Big Watermelon
 * @description 线程中断例子
 * @date 2024/02/20/08:54
 */
public class ThreadInterrupt {

    /**
     *  interrupt();        中断线程
     *  join();     等待中断
     *  isInterrupted();        判断是否中断
     * @param args
     */
    public static void main(String[] args) {
        Thread thread = new MyThread();

        System.out.println("Main start.");

        thread.start();

        try {
            //暂停一毫秒
            //TimeUnit.MILLISECONDS.sleep(1);
            TimeUnit.MILLISECONDS.sleep(1000);

            //发起中断线程
            thread.interrupt();

            //等待中断信号
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("Main end.");
    }
}
